/*******************************************************************
 *   > File Name: adc.c
 *   > Author: fly
 *   > Mail: XXXXXXXX@icode.com
 *   > Create Time: 2020年04月20日 星期一 23时00分16秒
 ******************************************************************/
#include "stdio.h"

#define TSADCCON0           0xE1700000
#define TSDATX0             0xE170000C
#define TSDATY0             0xE1700010
#define ADCMUX              0xE170001C

#define rTSADCCON0          (*(volatile unsigned int *)TSADCCON0)
#define rTSDATX0            (*(volatile unsigned int *)TSDATX0)
#define rTSDATY0            (*(volatile unsigned int *)TSDATY0)
#define rADCMUX             (*(volatile unsigned int *)ADCMUX)

//init adc controller
static void adc_init(void)
{
    rTSADCCON0 |= (1<<16);  // resolution set to 12bit
    rTSADCCON0  |= (1<<14);     // enable clock prescaler
    rTSADCCON0  &= ~(0xFF<<6);
    rTSADCCON0  |= (65<<6);     // convertor clock = 66/66M=1MHz, MSPS=200KHz
    rTSADCCON0  &= ~(1<<2);     // normal operation mode
    rTSADCCON0  &= ~(1<<1);     // disable start by read mode

    rADCMUX     &= ~(0x0F<<0);  // MUX select ADCIN0
}

static void delay(void)
{
    volatile unsigned int i,j;

    for(i=0; i<4000; i++)
        for(j=0; j<1000; j++);
}

void adc_test(void)
{
    unsigned int val = 0;

    adc_init();

    while(1){
        // A/D conversion starts by enable
        rTSADCCON0  |= (1<<0);
        
        // wait the End of conversion flag
        while (!(rTSADCCON0 & (1<<15)));
        
        //read the data, and print
        val = rTSDATX0;
        printf("x: bit14 = %d.\n\r", (val & (1<<14)));
        printf("x: adc value = %d.\n\r", (val & (0xFFF<<0)));

        val = rTSDATY0;
        printf("y: bit14 = %d.\n\r", (val & (1<<14)));
        printf("y: adc value = %d.\n\r", (val & (0xFFF<<0)));

        delay();
    }
}
